package controlLayer;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * The test class LoanControllerTest.
 *
 * @author  Gruppe E
 * @version 1.0
 */
public class LoanControllerTest
{
    private static int counter = 0;
    private String counterString = ""; //Integer.toString(counter);
    
    /**
     * Default constructor for test class LoanControllerTest
     */
    public LoanControllerTest()
    {
    }

    /**
     * Sets up the test fixture.
     *
     * Called before every test case method.
     */
    @Before
    public void setUp()
    {
        counter++;
    }

    /**
     * Tears down the test fixture.
     *
     * Called after every test case method.
     */
    @After
    public void tearDown()
    {
        
    }
    
    /**
     * Method testCreateLoan
     * 
     * Tests CreateLoan
     *
     */
    @Test
    public void testCreateLoan()
    {
        controlLayer.LoanController loanCont1 = new controlLayer.LoanController();
        controlLayer.DVDController dVDContr1 = new controlLayer.DVDController();
        controlLayer.PersonController personCo1 = new controlLayer.PersonController();
        assertEquals(true, personCo1.createPerson("test", "test", "test", "test", "555"));
        assertEquals(true, dVDContr1.createDVD("abc", "test", "test", "test"));
        assertEquals("confirmed", dVDContr1.createCopy("abc", "789", "test", 123));
        assertEquals("confirmed", dVDContr1.createCopy("abc", "987", "test", 123));
        assertEquals("confirmed", dVDContr1.createCopy("abc", "187", "test", 123));
        counterString = Integer.toString(counter);
        assertEquals("confirmed", loanCont1.createLoan(counterString, "test", "test", "555", "abc", "789")); //opret lån
        assertEquals("loanExists", loanCont1.createLoan(counterString, "test", "test", "555", "abc", "187")); //opret samme lån
        assertEquals("DVDRentOut", loanCont1.createLoan(counterString, "test", "test", "555", "abc", "789")); //opret ved samme copy
        assertEquals(false, dVDContr1.isHome("abc", "789")); //tjekker om copy er udlejet
        assertEquals("personNotFound", loanCont1.createLoan(counterString, "test", "test", "111", "aaa", "777")); //opret lån med forkert phone
        counter++;
        counterString = Integer.toString(counter);
        assertEquals("confirmed", loanCont1.createLoan(counterString, "test", "test", "555", "abc", "987")); //samme telefon nummer og barcode
        //assertEquals(true, loanCont1.deleteLoan("234"));
        counter++;
        counterString = Integer.toString(counter);
        assertEquals("DVDNotFound", loanCont1.createLoan(counterString, "test", "test", "555", "aaa", "789")); //samme serial nummer
        //assertEquals(true, loanCont1.deleteLoan("123"));
        assertEquals(true, personCo1.deletePerson("555"));
        assertEquals(true, dVDContr1.deleteCopy("abc", "987"));
        assertEquals(true, dVDContr1.deleteCopy("abc", "789"));
        assertEquals(true, dVDContr1.deleteCopy("abc", "187"));
        assertEquals(true, dVDContr1.deleteDVD("abc"));
    }
    
    /**
     * Method testUpdateLoan
     * 
     * Tests UpdateLoan
     *
     */
    @Test
    public void testUpdateLoan()
    {
        controlLayer.LoanController loanCont1 = new controlLayer.LoanController();
        controlLayer.DVDController dVDContr1 = new controlLayer.DVDController();
        controlLayer.PersonController personCo1 = new controlLayer.PersonController();
        assertEquals(true, personCo1.createPerson("test", "test", "test", "test", "555"));
        assertEquals(true, dVDContr1.createDVD("abc", "test", "test", "test"));
        assertEquals("confirmed", dVDContr1.createCopy("abc", "789", "test", 123));
        counter++;
        counterString = Integer.toString(counter);
        String updateString = Integer.toString(counter++);
        assertEquals("confirmed", loanCont1.createLoan(counterString, "test", "test", "555", "abc", "789")); //opret lån
        assertEquals(true, loanCont1.updateLoan(counterString, "test1", "test1", updateString)); // opdatater lån
        assertEquals(false, loanCont1.updateLoan("4000", "test1", "test1", updateString)); // opdaterer lån med forkert oldId
        counter++;
        counter++;
        //assertEquals(true, loanCont1.deleteLoan("234"));
        assertEquals(true, personCo1.deletePerson("555"));
        assertEquals(true, dVDContr1.deleteCopy("abc", "789"));
        assertEquals(true, dVDContr1.deleteDVD("abc"));
    }
    
    /**
     * Method testUpdateLoan2
     * 
     * Tests UpdateLoan again
     *
     */
    @Test
    public void testUpdateLoan2()
    {
        controlLayer.LoanController loanCont1 = new controlLayer.LoanController();
        controlLayer.DVDController dVDContr1 = new controlLayer.DVDController();
        controlLayer.PersonController personCo1 = new controlLayer.PersonController();
        assertEquals(true, personCo1.createPerson("test", "test", "test", "test", "555"));
        assertEquals(true, dVDContr1.createDVD("abc", "test", "test", "test"));
        assertEquals("confirmed", dVDContr1.createCopy("abc", "789", "test", 123));
        counter++;
        counterString = Integer.toString(counter);
        assertEquals("confirmed", loanCont1.createLoan(counterString, "test", "test", "555", "abc", "789")); //opret lån
        assertEquals(true, loanCont1.updateLoan(counterString, "test1", "nyPeriod", counterString)); // opdatater lån til samme id
        assertNotNull(loanCont1.findLoan(counterString)); //find lån med rigtigt id
        assertEquals("nyPeriod", loanCont1.findLoan(counterString).getPeriod()); //find lån med rigtigt id
        assertEquals(true, personCo1.deletePerson("555"));
        assertEquals(true, dVDContr1.deleteCopy("abc", "789"));
        assertEquals(true, dVDContr1.deleteDVD("abc"));
        
    }
    
    /**
     * Method testFindLoan
     * 
     * Tests findLoan
     *
     */
    @Test
    public void testFindLoan()
    {
        controlLayer.LoanController loanCont1 = new controlLayer.LoanController();
        controlLayer.DVDController dVDContr1 = new controlLayer.DVDController();
        controlLayer.PersonController personCo1 = new controlLayer.PersonController();
        assertEquals(true, personCo1.createPerson("test", "test", "test", "test", "555"));
        assertEquals(true, dVDContr1.createDVD("abc", "test", "test", "test"));
        assertEquals("confirmed", dVDContr1.createCopy("abc", "789", "test", 123));
        counter++;
        counterString = Integer.toString(counter);
        assertEquals("confirmed", loanCont1.createLoan(counterString, "test", "test", "555", "abc", "789")); //opret lån
        assertNotNull(loanCont1.findLoan(counterString)); //find lån med rigtigt id
        assertNull(loanCont1.findLoan("2000")); //find lån med forkert id
        //assertEquals(true, loanCont1.deleteLoan("123"));
        assertEquals(true, personCo1.deletePerson("555"));
        assertEquals(true, dVDContr1.deleteCopy("abc", "789"));
        assertEquals(true, dVDContr1.deleteDVD("abc"));
    }
    
    /**
     * Method testDeleteLoan
     * 
     * Tests DeleteLoan
     *
     */
    @Test
    public void testDeleteLoan()
    {
        controlLayer.LoanController loanCont1 = new controlLayer.LoanController();
        controlLayer.DVDController dVDContr1 = new controlLayer.DVDController();
        controlLayer.PersonController personCo1 = new controlLayer.PersonController();
        assertEquals(true, personCo1.createPerson("test", "test", "test", "test", "555"));
        assertEquals(true, dVDContr1.createDVD("abc", "test", "test", "test"));
        assertEquals("confirmed", dVDContr1.createCopy("abc", "789", "test", 123));
        counter++;
        counterString = Integer.toString(counter);
        assertEquals("confirmed", loanCont1.createLoan(counterString, "test", "test", "555", "abc", "789")); //opret lån
        assertEquals(false, loanCont1.deleteLoan("8210")); //delete lån der ikke findes
        assertEquals(true, loanCont1.deleteLoan(counterString)); //delete lån eller rettere: set state til false
        assertEquals(false, loanCont1.getState(counterString)); //test om state er blevet sat til false
        assertEquals(true, personCo1.deletePerson("555"));
        assertEquals(true, dVDContr1.deleteCopy("abc", "789"));
        assertEquals(true, dVDContr1.deleteDVD("abc"));
    }
}
